﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace V5_Apps.Core {
    public class PluginHelper {

        private static readonly PluginHelper m_Instance = new PluginHelper();
        public static PluginHelper Instance {
            get { return m_Instance; }
        }

        public class ModelPluginItem {
            private string _pluginDir;
            /// <summary>
            /// 插件目录
            /// </summary>
            public string PluginDir {
                get { return _pluginDir; }
                set { _pluginDir = value; }
            }

            private string _text;
            /// <summary>
            /// 文字说明
            /// </summary>
            public string Text {
                get { return _text; }
                set { _text = value; }
            }

            private string _RunTime;
            /// <summary>
            /// 运行类库
            /// </summary>
            public string RunTime {
                get { return _RunTime; }
                set { _RunTime = value; }
            }

            private string _commond;
            /// <summary>
            /// 命令
            /// </summary>
            public string Commond {
                get { return _commond; }
                set { _commond = value; }
            }

            private List<ModelMenuItem> _ListMenu = new List<ModelMenuItem>();
            /// <summary>
            /// 菜单集合
            /// </summary>
            public List<ModelMenuItem> ListMenu {
                get { return _ListMenu; }
                set { _ListMenu = value; }
            }

            private List<ModelToolItem> _ListTool;
            /// <summary>
            /// 工具集合
            /// </summary>
            public List<ModelToolItem> ListTool {
                get { return _ListTool; }
                set { _ListTool = value; }
            }
        }
        /// <summary>
        /// 菜单
        /// </summary>
        public class ModelMenuItem {
            private string _text;
            /// <summary>
            /// 文本
            /// </summary>
            public string Text {
                get { return _text; }
                set { _text = value; }
            }

            private string _icons;
            /// <summary>
            /// 图标
            /// </summary>
            public string Icons {
                get { return _icons; }
                set { _icons = value; }
            }

            private string _commond;
            /// <summary>
            /// 命令
            /// </summary>
            public string Commond {
                get { return _commond; }
                set { _commond = value; }
            }
        }
        /// <summary>
        /// 工具
        /// </summary>
        public class ModelToolItem {
            private string _text;
            /// <summary>
            /// 文本
            /// </summary>
            public string Text {
                get { return _text; }
                set { _text = value; }
            }

            private string _icons;
            /// <summary>
            /// 图标
            /// </summary>
            public string Icons {
                get { return _icons; }
                set { _icons = value; }
            }

            private string _commond;
            /// <summary>
            /// 命令
            /// </summary>
            public string Commond {
                get { return _commond; }
                set { _commond = value; }
            }
        }

        /// <summary>
        /// 加载所有插件
        /// </summary>
        /// <returns></returns>
        public List<ModelPluginItem> LoadAllPluginList() {
            List<ModelPluginItem> list = new List<ModelPluginItem>();
            string PluginDirPath = AppDomain.CurrentDomain.BaseDirectory + "Plugins\\";

            string[] Dirs = Directory.GetDirectories(PluginDirPath);

            foreach (string dir in Dirs) {
                var d = new DirectoryInfo(dir);
                ModelPluginItem model = new ModelPluginItem();
                XmlDocument xml = new XmlDocument();
                xml.Load(dir + "\\Plugins.config");
                model.PluginDir = d.Name;
                XmlNode xmlNode = xml.SelectSingleNode("plugin/runtime");
                if (xmlNode != null) {
                    model.RunTime = xmlNode.InnerText;
                    model.Commond = xmlNode.Attributes["commond"].Value;
                    model.Text = xmlNode.Attributes["text"].Value;
                }
                list.Add(model);
            }

            return list;
        }
    }
}
